const express = require("express");
// 创建路由器对象
const o = express.Router();
// 引入jwt模块
const jwt = require("jsonwebtoken");

// 引入密钥 和 过期时间
const { secretKey, expiresIn } = require("../config/config");
const { verifyToken } = require("../middlewares/Auth");
// 引入第三方连接池模块
const pool = require("../pool/pool");
//****************************** */
o.post("/submitorder", (req, res, next) => {
  let obj = req.body;
  console.log(obj);
  // let sql = `insert into order values (${obj.order_id},"${obj.order_code_id}","${obj.order_renttime}","${obj.order_returntime}","${obj.order_carlocation}",${obj.ocar_id},${obj.ouser_id},"${obj.order_success}",${obj.order_day},${obj.order_price})`;
  let sql01 = "insert into car_order values (?,?,?,?,?,?,?,?,?,?)";
  // let sql01 ='INSERT INTO order VALUES  (?)'
  console.log(sql01);
  let orderday = parseInt(obj.order_day);
  let orderprice = parseInt(obj.order_price);
  let carid = parseInt(obj.ocar_id);
  let userid = parseInt(obj.ouser_id);
  console.log(orderday);
  const token = (req.headers.authorization || "").slice(7);
  if (verifyToken(token)) {
    pool.query(
      sql01,
      [,
        obj.order_code_id,
        obj.order_renttime,
        obj.order_returntime,
        obj.order_carlocation,
        carid,
        userid,
        obj.order_success,
        orderday,
        orderprice,
      ],
      (err, data) => {
        if (err) { next(err);return;}
        console.log(data);
        res.send({ code: 200, msg: "提交订单成功", data: data });
      }
    );
  }else{
    res.send({"code":401,"msg":"登录已失效请重新登录!"})
  }
});

// 获取订单信息
o.get("/getorderdata:id?", (req, res, next) => {
  let obj = req.query;
  console.log(obj);
  let sql = "select * from car_order where order_id = ?";
  pool.query(sql, [obj.id], (err, data) => {
    if (err) {
      next(err);
      return;
    }

    res.send({ code: 200, data: data[0] });
  });
});
// 支付现金
o.get("/ordersuc:id?", (req, res, next) => {
  console.log(req.query.id);
  let sql = "update car_order set order_success = ? where order_id = ?";
  const token = (req.headers.authorization || "").slice(7);
  if(verifyToken(token)){
    pool.query(sql, ["已支付", req.query.id], (err, data) => {
      if (err) {
        next(err);
        return;
      }
      console.log(data);
      if (data.affectedRows == 1) {
        res.send({ code: 200 });
      }
    });
  }else{
    res.send({"code":401,"msg":"登录已失效请重新登录!"})
  }

});
// 根据订单号获取订单详情信息
o.get("/orderdetail:id?", (req, res, next) => {
  let obj = req.query;
  let sql = "select * from car_order where order_id = ?";
  const token = (req.headers.authorization || "").slice(7);
  if(verifyToken(token)){
    pool.query(sql, [obj.id], (err, data) => {
      if (err) {
        next(err);
        return;
      }
      // console.log(data[0])
      let orderdata = data[0];
      let carId = data[0].ocar_id;
      let userId = data[0].ouser_id;
      if (data.length !== 0) {
        let sql01 = "select * from car where car_id = ? ";
        pool.query(sql01, [carId], (err, cardata) => {
          // console.log(cardata)
          let Car = cardata[0];
          if (cardata.length != 0) {
            let sql02 = "select * from user where user_id = ?";
            pool.query(sql02, [userId], (err, udata) => {
              res.send({
                orderdata: orderdata,
                cardata: Car,
                userdata: udata[0],
              });
            });
          }
          // res.send({"cardata":cardata,"orderdata":orderdata})
        });
      }
      // res.send({"code":200,"data":data[0]})
    });
  }else{
    res.send({"code":401,"msg":"登录已失效请重新登录"})
  }

});
//*************************//
module.exports = o;
